package com.bulletelc.apollopre.module.main.center.setting.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.framework.BaseActivity;
import com.bulletelc.apollopre.utils.RegExUtil;
import com.bulletelc.apollopre.widget.FoolBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by CX on 2018/1/16.
 */

public class ModifyMobileActivity extends BaseActivity implements View.OnClickListener, FoolBar.ClickCallback {

    private FoolBar foolBar;
    private Button btnPost;
    private EditText etPhone;

    @Override
    public int getLayoutRes() {
        return R.layout.modify_mobile_activity;
    }

    @Override
    public void assignViews() {
        foolBar = findViewById(R.id.flBar);
        btnPost = findViewById(R.id.btn_post);
        etPhone = findViewById(R.id.et_new_phone);
    }

    @Override
    public void setListener() {
        foolBar.setClickCallback(this);
        btnPost.setOnClickListener(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        foolBar.setTitle(this.getResources().getString(R.string.setting_modify_mobile_number));
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_post:
                postSettingPhone(NetWorkPath.POST_REPHONE);
                break;
        }
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    /**
     * 提交用户修改
     * @param urlString
     */
    private void postSettingPhone(String urlString) {
        String token = MyApplication.getToken();
        if(token == null || token.length() <= 0) {
            Toast.makeText(getApplicationContext(), "登陆过期", Toast.LENGTH_LONG).show();
            return;
        }
        if (!RegExUtil.isMobileSimple(etPhone.getText().toString().trim())) {
            etPhone.setError(getString(R.string.hint_input_correct_mobile));
            return;
        }
        JSONObject obj = new JSONObject();
        JSONObject objSub = new JSONObject();
        try {
            objSub.put("mobile_number", etPhone.getText().toString());
            obj.put("SettingPhoneForm", objSub);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(MediaType.parse(GlobalConfig.CONTENT_TYPE_JSON),obj.toString());
        OkGo.<String>post(urlString)
                .headers("Authorization", GlobalConfig.AUTH_HEAD + token)
                .upRequestBody(body)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }

    /**
     * 解析返回结果
     * @param response
     */
    private void parseData(Response<String> response) {
        if(response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
//                LogUtil.i(response.body().toString());
                if(GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code").toString())) {
                    this.finish();
                    Toast.makeText(this, objMeta.getString("msg"), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, objMeta.getString("msg"), Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }

}
